package wash;

import com.mongodb.client.MongoCollection;
import mongo.MongoTest;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.bson.Document;
import test.StringTest;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-02-23 14:41
 */
public class RbkWash {

    public static MongoCollection<Document> collection = MongoTest.nginxFlow.getCollection("nginx_flow");

    public static void main(String[] args) throws IOException {
//        Instant i = Instant.now();
//        System.out.println(i);
        getFile("C:\\Users\\ucmed\\Desktop\\host.access.log.1");
    }

    public static JSONObject read(String s) {
        Pattern p = Pattern.compile("(\\d.*?\\.\\d{1,5})(?=\\s-)|(?<=\\[)(.*?)(?=\\])|(?<=\\s\\\")(.*?)(?=\\\")|(?<=\\\"\\s)(\\d*)(?=\\s)|(?<=\\s)(\\d*)(?=\\s\\\")|(?<=\\s)(\\d*)(?=\\s\\\")");
        Matcher matcher = p.matcher(s);
        List<String> list = new ArrayList<String>();
//        System.out.println(s);
        while (matcher.find()) {
            list.add(matcher.group());
        }
//        System.out.println(list);
        JSONObject obj = new JSONObject();
        try {
            if (list.size() == 10) {
                String request = list.get(2);
                String[] requests = request.split(" ");
                obj.put("request_method", requests[0]); // 请求方法
                obj.put("request_url", requests[1]); // 请求路径
                obj.put("request_deal", requests[2]); // 请求 协议
                obj.put("ip", list.get(0)); // ip
                obj.put("time", list.get(1)); // nginx记录时间
                obj.put("status", list.get(3)); // 状态
                obj.put("body_bytes_sent", list.get(4)); // out_size
                obj.put("http_referer", list.get(5)); // 请求来源
                obj.put("http_user_agent", list.get(6)); // 客户端信息
                if (StringUtils.isNotBlank(list.get(8))) {
                    String req = list.get(8);
                    if (-1 != req.indexOf("=") && req.length() > 5) {
                        req = req.substring(req.indexOf("=") + 1, req.length());
                    }
                    obj.put("req", req);
                }
                obj.put("resp", list.get(9));
            } else {
                throw new Exception("日志异常");
            }
        } catch (Exception e) { // 记录不能解析的日志
            // TODO
            System.out.println(e.getMessage() + "错误：" + s);
        }
        return obj;
    }

    public static void getFile(String fileName) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(fileName)));
        int i = 0;
        JSONObject obj;
        for (String s = br.readLine(); s != null; s = br.readLine()) {
//            System.out.println(i + " : " + s);
//            if (i == 200) {
//                return;
//            }
            obj = read(s);
            if (-1 != obj.optString("req").indexOf("{")) {
                System.out.println(StringTest.decode2(obj.optString("req")));
            }
//            if (obj.optString("request_url").contains("js")) {
//                System.out.println(obj.optString("resp"));
//            }
//            MongoTest.insertOne(collection, obj);
        }
        br.close();
    }

    public void read2() {
        Pattern p = Pattern.compile("((.*?)(\\s-))\\s\\[(.*?)\\]\\s");
    }
}
